<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
		<el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-row>
        <el-col :span="20">
          <el-form-item label="会员名称" prop="customerName">
            <el-input v-model="dataForm.customerName" placeholder="会员名称" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="0">
            <el-button @click="openCustomer">选择</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="会员卡号" prop="customerCard">
        <el-input v-model="dataForm.customerCard" placeholder="会员卡号" disabled></el-input>
      </el-form-item>
      <el-form-item label="会员原有预付款">
        <el-input v-model="dataForm.originalMoney" placeholder="会员原有预付款" disabled></el-input>
      </el-form-item>
      <el-form-item label="金额" prop="money">
        <el-input v-model="dataForm.money" placeholder="金额"></el-input>
      </el-form-item>
      <el-form-item label="充值部门">
        <el-input v-model="dataForm.deptName" placeholder="充值部门" disabled></el-input>
      </el-form-item>
      <el-form-item label="充值人">
        <el-input v-model="dataForm.operationUserName" placeholder="充值人" disabled></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="ischeck">确定</el-button>
    </span>
    <!-- 弹窗，查询顾客 -->
    <search-customer v-if="searchCustomerVisible" ref="searchCustomer" @refreshForm="getCustomerData"></search-customer>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import * as check from '@/util/check/check'
  import { getObj, addObj, putObj } from '@/api/eims/advancepayment'

  import SearchCustomer from '@/components/business/customer-search' // 查询顾客

  export default {
    data() {
      return {
        visible: false,
        ischeck: false,
        searchCustomerVisible: false,
        dataForm: {
          id: 0,
          customerId: '',
          customerName: '',
          customerCard: '',
          money: '',
          deptId: '',
          unitId: '',
          tenantId: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: '',
          delFlag: ''
        },
        dataRule: {
          customerName: [{ required: true, message: '会员不能为空', trigger: 'blur' }],
          customerCard: [{ required: true, message: '会员卡号不能为空', trigger: 'blur' }],
          money: [
            { required: true, message: '金额不能为空', trigger: 'blur' },
            { validator: check.checkIsNumber,trigger: 'blur'}
          ],
          deptId: [{ required: true, message: '销售部门/充值部门不能为空', trigger: 'blur' }]
        }
      }
    },
    computed: {
      ...mapGetters(['userInfo'])
    },
    components: {
      SearchCustomer
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        if (this.dataForm.id == 0) {
          this.dataForm.operationUser = this.userInfo.id
          this.dataForm.operationUserName = this.userInfo.realName
          this.dataForm.deptId = this.userInfo.lessDeptId
          this.dataForm.deptName = this.userInfo.lessDeptName
        }

        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.ischeck = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              });
            } else {
              addObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.ischeck = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            }
          }else{
            this.ischeck = false
          }
        })
      },
      openCustomer() {
        this.searchCustomerVisible = true
        this.$nextTick(() => {
          this.$refs.searchCustomer.init()
        })
      },
      // 获取顾客信息
      getCustomerData(data) {
        this.dataForm.originalMoney = data.advancePayment || 0
        this.dataForm.customerId = data.id
        this.dataForm.customerCard = data.customerCard
        this.dataForm.customerName = data.name
      },
    }
  }
</script>
